package com.jzy.message.activities.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.R;
import com.jzy.message.activities.base.AppConfig;
import com.jzy.message.activities.bean.ChengYuanBean;
import com.jzy.message.activities.bean.addBean;
import com.jzy.message.activities.messegeQunLiaoActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class addpopleAdapter extends XRecyclerView.Adapter<addpopleAdapter.ViewHoler>{
    private List<addBean.DataBean> bean;
    private Context context;
    public List list_temp=new ArrayList();
    private ChengYuanBean chengyuan;

    public boolean[] flag;
    public boolean[] flag1;//当前群成员
    public addpopleAdapter(List<addBean.DataBean> list, messegeQunLiaoActivity messegeQunLiaoActivity, ChengYuanBean serInfo) {
        bean=list;
        context=messegeQunLiaoActivity;
        chengyuan=serInfo;
        if (list!=null&&list.size()>0){
            flag= new boolean[list.size()+1];
        }
        if(serInfo!=null&&serInfo.getData().size()>0){
            flag1= new boolean[serInfo.getData().size()+1];

        }

    }

    @Override
    public addpopleAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_friends_item, parent, false);
        addpopleAdapter.ViewHoler holder = new addpopleAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final addpopleAdapter.ViewHoler holder, final int position) {
        String name=bean.get(position).getUserName()==
                null?"":bean.get(position).getUserName();
        holder.tv_name.setText(name);
        String nick=bean.get(position).getNick()==
                null?"":bean.get(position).getNick();
        String Url= bean.get(position).getAvatar()== null?"wu":
                bean.get(position).getAvatar();

            Picasso.with(context).load(AppConfig.JASON_SERVICE_URL_TOUXIANG+Url)
                    .placeholder(R.drawable.touxiang).into(holder.imge);

        holder.tv_nick.setText(nick);

        holder.checkbox.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器，传入参数null
        holder.checkbox.setChecked(flag[position]);//用数组中的值设置CheckBox的选中状态
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if(isChecked)
                {
                    flag[position] = isChecked;
                    list_temp.add(String.valueOf(bean.get(position).getUserId()));
                }
                else
                {
                    list_temp.remove(String.valueOf(bean.get(position).getUserId()));
                    flag[position] = false;
                }
            }
        });

        int userid=bean.get(position).getUserId();//所有好友
        for(int i=0;i<chengyuan.getData().size();i++){ //群本身的成员
            if(userid==chengyuan.getData().get(i).getUserid()){ //如果有相同的id
                holder.checkbox.setChecked(true);
             /*   holder.checkbox.setClickable(false);
                holder.checkbox.setEnabled(false);*/
            }
        }
    }

    @Override
    public int getItemCount() {
        return bean.size();
    }

    public class ViewHoler extends RecyclerView.ViewHolder {

        private final TextView tv_name;
        private final TextView tv_nick;
        private final ImageView imge;
        private final CheckBox checkbox;

        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_nick = (TextView)itemView.findViewById(R.id.tv_nick);
            imge = (ImageView)itemView.findViewById(R.id.imge);
            checkbox = (CheckBox)itemView. findViewById(R.id.checkbox);
        }
    }
}
